package com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.Constants;
import com.longdaji.decoration.DecorationApplication;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.GoodsCommentAdapter;
import com.longdaji.decoration.adapter.PhotoAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.AddComment;
import com.longdaji.decoration.bean.GoodsComment;
import com.longdaji.decoration.data.bean.GoodsImagesBean;
import com.longdaji.decoration.utils.BottomDialog;
import com.longdaji.decoration.utils.DialogMaker;
import com.longdaji.decoration.utils.EasyProgressDialog;
import com.longdaji.decoration.utils.LogUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class EvaluateActivity extends BaseActivity implements EvaluateContract.IView {

    private static final String TAG = "EvaluateActivity";

    private ProgressDialog pd;

    /*
    * 一次评论一个商品
    * */
    @Inject
    EvaluateContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.rv_photo)
    RecyclerView rvPhoto;
    @BindView(R.id.iv_anonymous)
    ImageView ivAnonymous;
    @BindView(R.id.iv_high_comment)
    ImageView ivHighComment;
    @BindView(R.id.iv_medium_comment)
    ImageView ivMediumComment;
    @BindView(R.id.iv_negative_comment)
    ImageView ivNegativeComment;
    @BindView(R.id.iv_goods_pic)
    ImageView ivGoodsPic;
    @BindView(R.id.et_content)
    EditText etContent;


    private static final int REQUEST_CODE_CHOOSE = 23;
    private PhotoAdapter mAdapter;
    private List<Uri> mUriList = new ArrayList<>();
    private boolean isAnonymous = true;

    private boolean isHighComment = false;
    private boolean isMediumComment = false;
    private boolean isNegativeComment = false;

    private BottomDialog mPhotoDialog;

    private String orderNo;
    private int goodsId;
    private boolean isPhotoAdded = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);

        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("发表评价");

        initView();
    }

    private void initView() {
        Intent extras = getIntent();
        orderNo = extras.getStringExtra(Constants.ORDER_NO);
        goodsId = extras.getIntExtra(Constants.GOODS_ID, 0);
        mPresenter.initGoods(goodsId);

        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return 1;
            }
        });
        rvPhoto.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new PhotoAdapter(mUriList);
        mAdapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
            @Override
            public void addPhoto() {
                createPhotoDialog();

            }
        });
        rvPhoto.setAdapter(mAdapter);
    }

    @OnClick({R.id.rl_tool_back, R.id.tv_finish, R.id.iv_anonymous, R.id.ll_high_comment, R.id.ll_medium_comment, R.id.ll_negative_comment})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.tv_finish:
                // TODO: 2018/4/10 0010 添加评价
//                DialogMaker.showProgressDialog(this, null);
                showProgressDialog();
                mPresenter.onFinishClick(isPhotoAdded, etContent.getText().toString(), goodsId, orderNo, mUriList);
                break;
            case R.id.iv_anonymous:
                mPresenter.onAnonymousClick(isAnonymous);
                break;
            case R.id.ll_high_comment:
                mPresenter.onCommentClick(Constants.HIGH);
                break;
            case R.id.ll_medium_comment:
                mPresenter.onCommentClick(Constants.MEDIUM);
                break;
            case R.id.ll_negative_comment:
                mPresenter.onCommentClick(Constants.NEGATIVE);
                break;
            default:
                break;
        }
    }

    private void showProgressDialog() {
        if (pd == null) {
            pd = new ProgressDialog(this);
            pd.setMessage("正在上传评论");
            pd.setCanceledOnTouchOutside(false);
        }
        if (!pd.isShowing()) {
            pd.show();
        }
    }

    private void hideProgressDialog() {
        if (pd != null && pd.isShowing())
            pd.dismiss();
    }


    @Override
    public void showAnonymous(boolean setAnonymous) {
        if (setAnonymous) {
            isAnonymous = true;
            ivAnonymous.setImageResource(R.mipmap.iv_checked);
        } else {
            isAnonymous = false;
            ivAnonymous.setImageResource(R.mipmap.iv_unchecked);
        }
    }

    @Override
    public void showCommentChosen(int degree) {
        switch (degree) {
            case Constants.HIGH:
                refreshChosenComment();
                isHighComment = true;
                ivHighComment.setImageResource(R.mipmap.high_comment_after);
                break;
            case Constants.MEDIUM:
                refreshChosenComment();
                isMediumComment = true;
                ivMediumComment.setImageResource(R.mipmap.medium_comment_after);
                break;
            case Constants.NEGATIVE:
                refreshChosenComment();
                isNegativeComment = true;
                ivNegativeComment.setImageResource(R.mipmap.negative_comment_after);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mUriList = Matisse.obtainResult(data);
            mAdapter.setData(mUriList);
            isPhotoAdded = true;
        }
    }

    private void refreshChosenComment() {
        if (isHighComment) {
            isHighComment = false;
            ivHighComment.setImageResource(R.mipmap.high_comment);
        }
        if (isMediumComment) {
            isMediumComment = false;
            ivMediumComment.setImageResource(R.mipmap.medium_comment);
        }
        if (isNegativeComment) {
            isNegativeComment = false;
            ivNegativeComment.setImageResource(R.mipmap.negative_comment);
        }
    }

    private void createPhotoDialog() {
        if (mPhotoDialog == null) {
            mPhotoDialog = new BottomDialog(this, "从手机相册选择");
            mPhotoDialog.setBottomDialogListener(new BottomDialog.BottomDialogListener() {
                @Override
                public void onItemOneClick() {
                    RxPermissions rxPermissions = new RxPermissions(EvaluateActivity.this);
                    rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    if (aBoolean) {
                                        Log.d("mytest", "add photo");
                                        Matisse.from(EvaluateActivity.this)
                                                .choose(MimeType.allOf())
                                                .countable(true)
                                                .theme(R.style.Matisse_Dracula)
                                                .maxSelectable(12)
                                                .imageEngine(new GlideEngine())
                                                .forResult(REQUEST_CODE_CHOOSE);
                                    }
                                }
                            });
                }

                @Override
                public void onItemTwoClick() {

                }

                @Override
                public void onItemThreeClick() {

                }
            });

        }
        mPhotoDialog.show();
    }

    @Override
    public void showGoodsImage(String goodsImageAddress) {
        Glide.with(this).load(goodsImageAddress).into(ivGoodsPic);
    }

    @Override
    public void showUploadFailed() {
        LogUtil.e(TAG, "图片上传失败");
        Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDoGoodsCommentSuccess() {
        LogUtil.d(TAG, "评论上传成功");
//        DialogMaker.dismissProgressDialog();
        hideProgressDialog();
        Toast.makeText(this, "评论上传成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, OrderDetailActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
//        finish();
    }

    /*
    * 一次评论多个商品
    * */
//    @Inject
//    EvaluateContract.IPresenter mPresenter;
//    @BindView(R.id.tv_tool_title)
//    TextView tvTitle;
//    @BindView(R.id.tv_finish)
//    TextView tvFinish;
//    @BindView(R.id.rv_goods_comment)
//    RecyclerView rvGoodsComment;
//  /*  @BindView(R.id.rv_photo)
//    RecyclerView rvPhoto;
//    @BindView(R.id.iv_anonymous)
//    ImageView ivAnonymous;
//    @BindView(R.id.iv_high_comment)
//    ImageView ivHighComment;
//    @BindView(R.id.iv_medium_comment)
//    ImageView ivMediumComment;
//    @BindView(R.id.iv_negative_comment)
//    ImageView ivNegativeComment;*/
//
//
//    private static final int REQUEST_CODE_CHOOSE = 23;
//    //    private PhotoAdapter mAdapter;
////    private List<Uri> mUriList = new ArrayList<>();
//
//    private GoodsCommentAdapter mAdapter;
////    private List<Orders.OrdersBean.OrderBean.ProductsBean> mProductList = new ArrayList<>();
//
//    private List<AddComment> mAddCommentList = new ArrayList<>();
//    private List<GoodsComment> mGoodsCommentList = new ArrayList<>();
//
//    private boolean isAnonymous = true;
//
//    private boolean isHighComment = false;
//    private boolean isMediumComment = false;
//    private boolean isNegativeComment = false;
//
//    private BottomDialog mPhotoDialog;
//    private String orderNo;
//    private int mItemPosition = -1;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_evaluate);
//
//        setUnBinder(ButterKnife.bind(this));
//        if (mPresenter != null)
//            mPresenter.setView(this);
//        tvTitle.setText("发表评价");
//
//        initView();
//    }
//
//    private void initView() {
//
//        orderNo = getIntent().getStringExtra(Constants.ORDER_NO);
//
////        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
////        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
////            @Override
////            public int getSpanSize(int position) {
////                return 1;
////            }
////        });
//        rvGoodsComment.setLayoutManager(new LinearLayoutManager(this));
//        mAdapter = new GoodsCommentAdapter(mAddCommentList);
//        mAdapter.setCommentListener(new GoodsCommentAdapter.CommentListener() {
//            @Override
//            public void onHighCommentClick(int position) {
////                mPresenter.onCommentClick(Constants.HIGH);
//            }
//
//            @Override
//            public void onMediumCommentClick(int position) {
////                mPresenter.onCommentClick(Constants.MEDIUM);
//            }
//
//            @Override
//            public void onNegativeCommentClick(int position) {
////                mPresenter.onCommentClick(Constants.NEGATIVE);
//            }
//
//            @Override
//            public void onAnonymousClick(int position) {
////                mPresenter.onAnonymousClick(isAnonymous);
//            }
//
//            @Override
//            public void onCreateDialogClick(int position) {
//                mItemPosition = position;
//                createPhotoDialog();
//            }
//        });
//       /* rvPhoto.setLayoutManager(new GridLayoutManager(this, 4));
//        mAdapter = new PhotoAdapter(mUriList);
//        mAdapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
//            @Override
//            public void addPhoto() {
//                createPhotoDialog();
//
//            }
//        });
//        rvPhoto.setAdapter(mAdapter);*/
//
//        rvGoodsComment.setAdapter(mAdapter);
//        mPresenter.initList(orderNo);
//    }
//
//
//    @OnClick({R.id.rl_tool_back, R.id.tv_finish})
//    void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.rl_tool_back:
//                finish();
//                break;
//            case R.id.tv_finish:
//                // TODO: 2018/4/10 0010 添加评价
//                DialogMaker.showProgressDialog(this, null);
//                mAdapter.setContent();
//                for (AddComment addComment : mAddCommentList) {
//                    LogUtil.d("itemContentTest", addComment.getCommentContent());
//                }
//                mPresenter.onFinishClick(orderNo, mAddCommentList);
//                break;
//            default:
//                break;
//        }
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
//            List<Uri> mUriList = Matisse.obtainResult(data);
////            mAdapter.setData(mUriList);
//            mAddCommentList.get(mItemPosition).setCommentImages(mUriList);
//            mAdapter.notifyDataSetChanged();
//        }
//
//    }
//
//
//    private void createPhotoDialog() {
//        if (mPhotoDialog == null) {
//            mPhotoDialog = new BottomDialog(this, "拍照", "从手机相册选择");
//            mPhotoDialog.setBottomDialogListener(new BottomDialog.BottomDialogListener() {
//                @Override
//                public void onItemOneClick() {
//                    Toast.makeText(EvaluateActivity.this, "未完成", Toast.LENGTH_SHORT).show();
//                }
//
//                @Override
//                public void onItemTwoClick() {
//                    RxPermissions rxPermissions = new RxPermissions(EvaluateActivity.this);
//                    rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                            .subscribe(new Consumer<Boolean>() {
//                                @Override
//                                public void accept(Boolean aBoolean) throws Exception {
//                                    if (aBoolean) {
//                                        Log.d("mytest", "add photo");
//                                        Matisse.from(EvaluateActivity.this)
//                                                .choose(MimeType.allOf())
//                                                .countable(true)
//                                                .theme(R.style.Matisse_Dracula)
//                                                .maxSelectable(5)
//                                                .imageEngine(new GlideEngine())
//                                                .forResult(REQUEST_CODE_CHOOSE);
//                                    }
//                                }
//                            });
//                }
//
//                @Override
//                public void onItemThreeClick() {
//
//                }
//            });
//        }
//        mPhotoDialog.show();
//    }
//
//    @Override
//    public void showInitList(List<AddComment> addCommentList) {
//        mAdapter.setData(addCommentList);
//    }
//
//    @Override
//    public void showFinishDoGoodsComment() {
//        DialogMaker.dismissProgressDialog();
//        finish();
//    }
//
//    @Override
//    public void showUploadFailed() {
//        LogUtil.d("mytest", "图片上传失败");
//        Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show();
//    }

}
